<?php


namespace app\common\model\mysql;


use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
use think\model\concern\SoftDelete;
use think\Paginator;

class Category extends Model
{
    use SoftDelete;

    protected $autoWriteTimestamp = true;

    /**
     * 新增
     * @param string $field
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNormalCategories($field = '*'){
        $where = [
            'status' => config('status.mysql.table_normal')
        ];
        return $this->where($where)->field($field)->select();
    }

    /**
     * 排序
     * @param $where
     * @param int $num
     * @return Paginator
     * @throws DbException
     */
    public function getLists($where, $num = 10){
        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];
        $result =  $this->where('status','<>',config('status,mysql.table_delete'))
            ->where($where)
            ->order($order)
            ->paginate($num);
        return $result;
    }

    /**
     * 根据id 更新数据
     */
    public function updateById($id,$data) {
        $data['update_time'] = time();
        return $this->where(['id' => $id])->save($data);
    }
}